//
//  CommonEffectHelper.m
//  collection
//
//  Created by Jing Zhang on 5/9/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "CommonEffectHelper.h"

@implementation CommonEffectHelper

+ (void)initNavigationTitle:(NSString *)title image:(UIImage *)image controller:(UIViewController *)viewController {
    
    if (image) {
        UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        viewController.navigationItem.titleView = imageView;
        [imageView release];
    }
    else if (title) {
        viewController.navigationItem.title = title;
    }
}

+ (void)initNavigationBackBarButton:(UIViewController *)viewController target:(id)target action:(SEL)action {
    
    // To do, customize back button
    UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* bgImg = [UIImage imageNamed:@"top_btn_back_off"];
    [backButton setBackgroundImage:bgImg forState:UIControlStateNormal];
    if (action) {
        [backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    else {
        [backButton addTarget:viewController.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    viewController.navigationItem.leftBarButtonItem = backBarButton;
    [backBarButton release];
}

+ (void)initNavigationLeftBarButton:(NSString *)text image:(UIImage *)image controller:(UIViewController *)viewController target:(id)target action:(SEL)action {
    
    UIBarButtonItem* leftBarButton = nil;
    
    if (image) {
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setBackgroundImage:image forState:UIControlStateNormal];
        [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        [leftButton release];
    }
    else if (text) {
        leftBarButton = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleBordered target:target action:action];
    }
    else {
        return;
    }
    
    viewController.navigationItem.leftBarButtonItem = leftBarButton;
    [leftBarButton release];
}

+ (void)initNavigationRightBarButton:(NSString *)text image:(UIImage *)image controller:(UIViewController *)viewController target:(id)target action:(SEL)action {
    
    UIBarButtonItem* rightBarButton = nil;
    
    if (image) {
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setBackgroundImage:image forState:UIControlStateNormal];
        [rightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        [rightButton release];
    }
    else if (text) {
        rightBarButton = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleBordered target:target action:action];
    }
    else {
        return;
    }
    
    viewController.navigationItem.leftBarButtonItem = rightBarButton;
    [rightBarButton release];
}

@end
